
#ifndef _COMMAND_H_
#define _COMMAND_H_

#include "Reader.h"
#include "Token.h"

enum {
	CH_IDLE,
	CH_UART,
	CH_HID,
	CH_QR,
	CH_NET_S,
	CH_NET_C
};

#pragma pack(1)

typedef struct XferFrameTag{
	unsigned char stx;
	unsigned char address;
	unsigned char code;
	unsigned short length;
	unsigned char buffer[256];
}XferFrame_T;

#pragma pack()

typedef struct OptCommandTag{
	uint8_t code;
	uint8_t op;
	uint8_t buffer[2];
}OptCommand;

typedef struct {
	uint8_t type;
	uint16_t length;
	uint8_t *data;
}DataBlock_T;

enum {
	TK_Slot_Card,
	TK_Slot_Key,
	TK_Slot_Qr,
	TK_Slot_MAX,
};

void Command_Token_Set(uint8_t slot, const Token_T* token);
const Token_T* Command_Token_Get(uint8_t* slot);
int Command_Token_Check(uint8_t slot);
void Command_Token_Clear(uint8_t slot);

void Command_Timeout(void);
void Command_SendFrame(unsigned char address, unsigned char cmd, unsigned char* buf, unsigned short len);
void Command_SendDataBlock(uint8_t channel, uint8_t stx, unsigned char address, unsigned char cmd, DataBlock_T* data);
void Command_CharSendFrame(uint8_t channel, uint8_t stx, unsigned char address, unsigned char cmd, unsigned char* buf, unsigned short len);

void Command_Process(void);

int Command_Receive(uint8_t channel, unsigned char ch);
void Command_InitReceie(void);
uint8_t Command_Channel(uint8_t channel);
int optProcess(unsigned char* buf, unsigned char len, unsigned char isResp);
int Command_Validate(unsigned char* buf, unsigned short len);

int Command_CharRawSend(uint8_t channel, uint8_t buf[], uint32_t len);
void Command_Rs485Out(CSN_T *csn);
void Command_Init(void);
void Command_Rs485OutKey(uint8_t kval);
void Command_SyncSend(CSN_T *csn);
int Command_Idle(uint8_t flag);


void DKCommand_ReportCSN(CSN_T* csn);

void DKCommand_ReportNAK(void);


#endif



